package com.idc.resource.service;

import com.idc.resource.dal.dto.TUserInfo;
import com.idc.resource.enums.AuthCodeEnum;
import com.idc.resource.enums.ErrorCodeEnum;
import com.idc.resource.enums.UserLevelEnum;
import com.idc.resource.enums.UserTypeEnum;
import com.idc.resource.exception.IdcServiceException;
import com.idc.resource.help.EmailHelp;
import com.idc.resource.help.model.AuthCodeConfig;
import com.idc.resource.help.model.EmailMsg;
import com.idc.resource.manager.UserManager;
import com.idc.resource.model.AuthCodeInfo;
import com.idc.resource.model.UserChildReqDto;
import com.idc.resource.model.UserRegisterReqDto;
import com.idc.resource.model.UserInfoResDto;
import com.idc.resource.util.BeanMapperUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户服务
 * Created by Lijing on 2017/7/11.
 */
@Slf4j
@Service
public class UserService {
    /** 用户管理 */
    @Autowired
    private UserManager userManager;
    /** 令牌管理 */
    @Autowired
    private TokenService tokenService;
    /** 验证码服务 */
    @Autowired
    private AuthCodeService authCodeService;
    /**
     * 用户注册
     * @param userInfoReqDto 需要注册的用户信息
     * @return 注册结果
     */
    public UserInfoResDto register(UserRegisterReqDto userInfoReqDto){
        try {
            if(!UserTypeEnum.BUYER.getCode().equals(userInfoReqDto.getUserType())&& StringUtils.isBlank(userInfoReqDto.getIspCer())){
                throw new IdcServiceException(ErrorCodeEnum.PARAM_ERROR.getCode(),ErrorCodeEnum.PARAM_ERROR.getDesc());
            }
            userManager.register(userInfoReqDto);
            TUserInfo sel = userManager.selectOne(userInfoReqDto.getUserId(),userInfoReqDto.getLoginPwd());
            UserInfoResDto userInfo = BeanMapperUtil.objConvert(sel,UserInfoResDto.class);
            //登录并获取登录令牌
            tokenService.loginIn(userInfo);
            //发送邮箱验证邮件
            authCodeService.sendEmailAuth(userInfo,userInfoReqDto.getCompanyEmail());
            return userInfo;
        }catch (IdcServiceException ise){
            throw ise;
        }catch (Exception e){
            log.error("用户注册失败，系统异常，原因:",e);
            throw new IdcServiceException(ErrorCodeEnum.SYSTEM_ERROR.getCode(),ErrorCodeEnum.SYSTEM_ERROR.getDesc());
        }
    }

    /**
     * 创建子用户
     * @param userChildReqDto 子用户请求参数
     * @return  创建结果
     */
    public UserInfoResDto createChild(UserChildReqDto userChildReqDto){
        UserInfoResDto userInfoResDto = tokenService.getLoginUser(userChildReqDto.getToken());
        if(UserLevelEnum.NORMAL.getCode().equals(userInfoResDto.getUserLv())){
            log.error("用户{},无权限创建子用户",userInfoResDto);
            throw new IdcServiceException(ErrorCodeEnum.AUTH_ERROR.getCode(),ErrorCodeEnum.AUTH_ERROR.getDesc());
        }
        TUserInfo childUser = BeanMapperUtil.objConvert(userChildReqDto,TUserInfo.class);
        childUser.setCompanyId(userInfoResDto.getCompanyId());
        childUser.setLoginStatus("0");
        childUser.setManagerId(userInfoResDto.getId());
        childUser.setUserLv(UserLevelEnum.NORMAL.getCode());
        userManager.addUser(childUser);
        return BeanMapperUtil.objConvert(childUser,UserInfoResDto.class);
    }


    public UserInfoResDto updateUserInfo(){
        return null;
    }

}
